/* -------------------------------------------------------------------------- *
   TestWinUnit - Maria Blees (maria.blees@microsoft.com)

   File: MockTestContainer.cpp
   This file/class are used in place of TestModule for testing the TestRunner
   class.
 * -------------------------------------------------------------------------- */

#include "MockTestContainer.h"

using namespace WinUnitLib;

MockTestContainer::MockTestContainer(char* test1, char* test2 /* NULL */, char* test3 /* NULL */)
:
_next(0)
{
    _tests[0] = test1;
    _tests[1] = test2;
    _tests[2] = test3;
};

bool MockTestContainer::GetFirst(TestIdentifier& testIdentifier)
{
    _next = 0;
    return (GetNext(testIdentifier));
}

bool MockTestContainer::GetNext(TestIdentifier& testIdentifier)
{
    char* nextTest = _tests[_next++];
    if (nextTest != NULL)
    {
        testIdentifier = TestIdentifier(NULL, nextTest, nextTest);
        return true;
    }
    return false;
}

#pragma optimize ( "" , off )
// The optimizer would totally get rid of this unnecessary divide-by-zero 
// in release builds, thereby not allowing the exception-handling code
// to be exercised.
void DivideByZero()
{
    int n = 5;
    int m = 5 - n;
    n = n / m;
}
#pragma optimize ( "" , on )

bool FailingTest()
{
    return false;
}

bool SuccessfulTest()
{
    return true;
}

bool MockTestContainer::ExecuteTest(
    const TestIdentifier& testIdentifier,
    wchar_t* buffer, 
    size_t cchBuffer)
{
    if (::strcmp("DivideByZero", testIdentifier.TestName) == 0)
    {
        DivideByZero();
        ::wcscpy_s(buffer, cchBuffer, L"Failed!"); 
        return false;
    }
    if (::strcmp("FailingTest", testIdentifier.TestName) == 0)
    {
        bool result = FailingTest();
        ::wcscpy_s(buffer, cchBuffer, L"FailingTest failed!");
        return result;
    }
    if (::strcmp("SuccessfulTest", testIdentifier.TestName) == 0)
    {
        bool result = SuccessfulTest();
        return result;
    }

    return true;
}
